Sugar - Five-way Rocker
Introduction
This is a five-way joystick, which can detect the values of the four directions of the X and Y horizontal axes and the vertical axis press. It is a relatively special I2C module.
Parameters
Dimension | 24 x 24 x 23 mm |
---|---|
Weight | 7 g |
Type | I2C |
Range | X: -255~255 Y: -255~255 Z: Pressed or not |
Circuit Connection
Connect the joystick module to the Robotbit Edu's blue I2C interface using a 4PIN terminal wire. Turn on the Robotbit power supply to see the red light at the bottom of the photosensitive module light up (the module is normally powered)
Coding
Using Kittenblock
You can concatenate strings (numbers will be converted to strings) using the Join block under the Operation block category.
Due to poor online interaction real-time performance, it is recommended to upload the program and run it on the Future board. If you are not sure how to upload offline, please refer to Quick Start
Note that the colon in the English character string block should be an English colon (please do not enter the Chinese input method)
Joystick Direction Value
value(dir)
- dir: Select direction
- 'x': X axis, return value range -255~255
- 'y': Y axis, return value range -255~255
state()
- Return value:
- Default state: 'none'
- Other states: Detect 5-way state, according to the trigger state can return "pressed", "left", "right", "up" or "down".
from future import *
from sugar import *
joystick = Joystick()
screen.sync = 0
while True:
screen.fill((0, 0, 0))
screen.text(str("X: ")+str(joystick.value('x')), x = 5, y = 10)
screen.text(str("Y: ")+str(joystick.value('y')), x = 5, y = 30)
if joystick.state() == 'pressed':
screen.text("Pressed", x = 5, y = 50)
screen.refresh()
Effect Display
Print the data of the X and Y axes on the Future Board screen. When the joystick is pressed, the third line of the screen will display Pressed
Using on Microbit
Programming platform
Microsoft MakeCode for micro:bit Use Makecode programming platform
Add Sugar Plugin
Search for Sugar in the extensions, click Add
Building Blocks-Function Description
Sr. No. | Building Block Image | Building Block Function |
---|---|---|
1 | Read Joystick Status | |
2 | Read Joystick Raw Value |
Circuit Connection
Microbit Interface | Cable | |||
---|---|---|---|---|
Sugar cube joystick module | I2C | Black PH2.0-4Pin interface cable |